package com.bestcem.xm.user.grpc.service;

import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.grpc.service.converter.CommonConverter;
import com.bestcem.xm.user.grpc.service.converter.UserRoleConverter;
import com.bestcem.xm.user.service.RoleService;
import com.bestcem.xm.user.service.dto.group.IdRelationDTO;
import com.bestcem.xm.user.service.dto.role.UserRoleDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色权限grpc
 *
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 13:56
 */
@Slf4j
@GrpcService
public class UserRoleGrpcService extends UserRoleServiceGrpc.UserRoleServiceImplBase {


    @Resource
    private RoleService roleService;

    @Resource
    private UserRoleConverter userRoleConverter;

    /**
     * 根据roleId查询role
     *
     * @param request
     * @param responseObserver
     * @return void
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/03/17 14:06
     */
    @Override
    public void getRole(GetRoleRequest request, StreamObserver<GetRoleResponse> responseObserver) {
        GetRoleResponse.Builder builder = GetRoleResponse.newBuilder();
        String roleId = request.getId();
        UserRoleDTO userRoleDTO = new UserRoleDTO();
        userRoleDTO.setRoleId(roleId);
        ServiceResult<List<UserRoleDTO>> result = roleService.selectByCondition(userRoleDTO);
        if (result.isSuccess()) {
            if (!CollectionUtils.isEmpty(result.getData())) {
                UserRoleDTO roleDTO = result.getData().get(0);
                // 将用户组信息转成grpc响应结果
                builder.setRole(userRoleConverter.userRoleDto2Role(roleDTO));
            }
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
        } else {
            // 查询失败
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();

    }

    /**
     * 通过查询条件批量一次性获取所有符合条件的Role
     *
     * @param request
     * @param responseObserver
     * @return void
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/03/17 14:07
     */
    @Override
    public void listRoles(ListRolesRequest request, StreamObserver<ListRolesResponse> responseObserver) {
        ListRolesResponse.Builder builder = ListRolesResponse.newBuilder();
        UserRoleDTO roleDTO = userRoleConverter.roleToUserRoleDTO(request.getRole());

        ServiceResult<List<UserRoleDTO>> result = roleService.selectByCondition(roleDTO);
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            builder.addAllRoles(userRoleConverter.userRoleDtoToRoleList(result.getData()));
        } else {
            //查询失败
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 复制旧公司的Role
     *
     * @param request
     * @param responseObserver
     * @return void
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/03/17 15:06
     */
    @Override
    public void listSampleRoleIds(ListSampleRoleIdsRequest request, StreamObserver<ListSampleRoleIdsResponse> responseObserver) {
        ListSampleRoleIdsResponse.Builder builder = ListSampleRoleIdsResponse.newBuilder();
        //获取用户Id
        String srcId = request.getSrcOrgId();
        String dstId = request.getDstOrgId();
        ServiceResult<List<IdRelationDTO>> serviceResult = roleService.listSampleRoleIds(srcId, dstId);
        if (serviceResult.isSuccess()) {
            //查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.addAllIdRelations(CommonConverter.idRelationDtosGrpcs(serviceResult.getData()));
        } else {
            //查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 根据公司id获取系统管理员角色id
     *
     * @param request
     * @param responseObserver
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/7/5 14:36
     */
    @Override
    public void getAdminRoleId(GetAdminRoleIdRequest request, StreamObserver<GetAdminRoleIdResponse> responseObserver) {
        GetAdminRoleIdResponse.Builder builder = GetAdminRoleIdResponse.newBuilder();
        ServiceResult<String> serviceResult = roleService.getAdminRole(request.getOrgId());
        if (serviceResult.isSuccess() && StringUtils.isNotBlank(serviceResult.getData())) {
            //查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.setId(serviceResult.getData());
        } else if (serviceResult.isSuccess() && StringUtils.isBlank(serviceResult.getData())) {
            // 未查询到
            builder.setStatus(RpcStatusResult.error(String.format("role not exist. org_id = %s", request.getOrgId())));
        } else {
            //查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
